/**
 * 
 */
package com.carnation.qa.socketserver.common.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.qa.socketserver.domain.Item;

/**
 * @author Nick
 * 
 */
public class TreeParser {

	private final Log log = LogFactory.getLog(getClass());

	private final String tabSeparator;
	private final String newLineSeparator;

	/**
	 * 默认采用'\t' AND '\n'
	 */
	public TreeParser() {
		this.tabSeparator = MppContext.getTabSeparator();
		this.newLineSeparator = MppContext.getNewlineSeparator();
	}

	/**
	 * @param tabSeparator
	 * @param newLineSeparator
	 */
	public TreeParser(String tabSeparator, String newLineSeparator) {
		this.tabSeparator = tabSeparator;
		this.newLineSeparator = newLineSeparator;
	}

	public List<Item> parseTree(String content) {
		List<Item> list = new ArrayList<Item>();
		if (log.isDebugEnabled()) {
			log.debug("tabSeparator=\"" + tabSeparator
					+ "\",newLineSeparator=\"" + newLineSeparator + "\"");
		}
		String[] lines = content.split(newLineSeparator);
		if (log.isDebugEnabled()) {
			log.debug("content split size=" + lines.length);
		}
		for (Integer i : getRootLineNumbers(lines)) {
			Item root = new Item(tabSeparator, 0, lines[i], null);

			iteratorTree(lines, root, i + 1, 1);
			if (log.isDebugEnabled()) {
				log.debug(root);
			}
			list.add(root);
		}
		return list;
	}

	private List<Integer> getRootLineNumbers(String[] lines) {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < lines.length; i++) {
			if (lines[i].length() > 0 && (!lines[i].startsWith(tabSeparator))) {
				list.add(i);
			}
		}
		return list;
	}

	// private int getRootLineNumber(String[] lines) {
	// int index = 0;
	// for (int i = 0; i < lines.length; i++) {
	// if (lines[i].trim().length() > 0) {
	// index = i;
	// break;
	// }
	// }
	// return index;
	// }

	/**
	 * 递归遍历
	 * 
	 * @param lines
	 *            所有行数组
	 * @param parent
	 *            上级节点对象
	 * @param lineNum
	 *            当前行标号
	 * @param tabCounts
	 *            缩进次数
	 */
	private void iteratorTree(String[] lines, Item parent, int lineNum,
			int tabCounts) {
		String line, prefix = buildPrefix(tabCounts);
		// if (log.isDebugEnabled()) {
		// log.debug("lineNum=" + lineNum + ",parent=" + parent);
		// }
		for (int i = lineNum; i < lines.length; i++) {
			line = lines[i];
			if (line.startsWith(prefix)) {
				if (line.charAt(prefix.length()) > 32) {
					Item item = new Item(tabSeparator, i, line.trim(), parent);
					parent.addItem(item);
					iteratorTree(lines, item, (i + 1), (tabCounts + 1));
				}
			} else {
				// 退出当前循环
				break;
			}
		}
	}

	private String buildPrefix(int times) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < times; i++) {
			sb.append(tabSeparator);
		}
		return sb.toString();
	}

}
